<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Crypt;
use DB;

class AdminsController extends Controller
{
    
    //add
    public function add()
    {

    	return view('admin.admins.add');
    }

    //insert
    public function insert(Request $request)
    {
    	//dd($request -> all());
    	// if(!$request -> input('name'))
    	// {
    	// 	return back() -> with(['name'=>'用户名不能为空']);
    	// }

    	$this ->validate($request,[
    			'username' => 'required',
    			'password' => 'required',
    			'repassword' => 'required|same:password',
    			'email' => 'required|email',
    			
    		],[
    			'username.required' => '用户名不能为空',
    			'password.required' => '密码不能为空',
    			'repassword.required' =>'确认密码不能为空',
    			'repassword.same' =>'两次密码不一样',
    			'email.required' =>'邮箱不能为空',
    			'email.email' => '邮箱格式不正确',
    			
    		]);
    	//处理好数据
    	$data = $request -> except('_token','repassword');
    	// dd($data);

    	//加密密码
    	$data['password'] = Crypt::encrypt($data['password']);
    	// dd($data);

    	$data['remember_token'] = str_random(70);
    	$time = date("Y-m-d H:i:s");
    	$data['created_at'] = $time;
    	$data['updated_at'] = $time;

    	//上传图片
    	if($request -> hasFile('img'))
    	{
    		
    		if($request -> file('img') -> isValid())
    	 	{
    	 		//获取扩展名
	    	
	    		$suffix = $request -> file('img') -> getClientOriginalExtension();
	    		//随机名称
	    		$fileName = time().mt_rand(100000,999999).'.'.$suffix;
	    		$request ->file('img') -> move('./uploads', $fileName);
	    		//处理image字段
    			$data['img'] = '/uploads/'.$fileName;

	    	}	
    	}

    	//添加数据
    	$res = DB::table('admins') -> insert($data);
    	if($res)
    	{
    		return redirect('/admin/admins/index') -> with(['info' => '添加成功']);

    	}else
    	{
    		return redirect('/admin/admins/index') -> with(['info' => '添加失败']);
    	}
	}

	//index
	 public function index(Request $request)
	 {
	 	//查询
	 	$num = $request -> input('num',1);
	 	$data = DB::table('admins')
	 		->where(function($query) use ($request){
	 			$query -> where('username', 'like', '%'.$request -> input('keyword').'%');
	 		})
	 		->paginate($num);
	 	
	 	
	 	return view('admin.admins.index', ['data' => $data, 'request'=>$request -> all()]);
	}

	// 编辑
	 public function edit($id)
	 {
	 	// return $id;
	 	$data = DB::table('admins') -> where('id', $id) -> first();
	 	// dd($data);
	 	return view('admin.admins.edit', ['data'=> $data]);
	 }

	//更新
	 public function update(Request $request)
	 {
	 	// dd($request -> all());
	 	$this -> validate($request,[
	 			'username' => 'required',
	 			'email' => 'required|email',
	 			
	 		],[
	 			'username.required' => '用户名不能为空',
	 			'email.required' => '邮箱不能为空',
	 			'email.email' => '邮箱格式不正确',
	 			
	 		]);
	 	$data = $request ->except('_token', 'id');
	 	$data['updated_at'] = date('Y-m-d H:i:s');

	 	// 执行更新
	 	$res = DB::table('admins') -> where('id', $request -> input('id'))-> update($data);
	 	//判断
	 	if($res)
	 	{
	 		return redirect('/admin/admins/index') -> with(['info' => '更新成功']);
	 	}else
	 	{
	 		return back() -> with(['info' => '更新失败']);
	 	}
	 }


	 //删除
	 public function delete($id)
	 {
	 	// return $id;
	 	$res = DB::table('admins') -> where('id', $id) -> delete();
	 	//判断
	 	if($res)
	 	{
	 		return redirect('/admin/admins/index') -> with(['info' => '删除成功']);
	 	}else
	 	{
	 		return back() -> with(['info' => '删除失败']);
	 	}
	 }


	 //ajaxUpdate
	 public function ajaxUpdate(Request $request)
	 {
	 	// return response() -> json(0);
	 	$id = $request -> input('id');
	 	//根据ID查数据
	 	$data = DB::table('admins') -> where('id', $id) ->first();
	 	//判断
	 	if($data -> status =='0')
	 	{
	 		$res = DB::table('admins') -> where('id', $id) ->update(['status'=> '1']);
	 		if($res)
	 		{
	 			return response() -> json(0);
	 		}else
	 		{
	 			return response() -> json(1);
	 		}
	 	}else
	 	{
	 		$res = DB::table('admins') -> where('id', $id) ->update(['status'=> '0']);
	 		if($res)
	 		{
	 			return response() -> json(2);
	 		}else
	 		{
	 			return response() -> json(3);
	 		}
	 	}

	 }


}
